package com.qixin.activiti.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;

/***
* @Description: 报销监听通用工具
* @author: CYS
* @date: 2022/3/8 10:30
*/
public class EaListenerUtil{

    /**
     * @Description: 属性值null，实体类动态设置默认值
     * @author: CYS
     * @date: 2022/2/18 11:55
     * @param object:
     * @param Class:
     * @Return: void
     */
    public static  void  objectDefault(Object object, Class Class)throws Exception{

        //获取所有类的属性集合
        Field[] list1=Class.getDeclaredFields();

        for(Field field:list1){
            //属性开头大写
            String replace = field.getName().substring(0, 1).toUpperCase()+ field.getName().substring(1);
            try {

                //动态调用方法
                Method method2=Class.getMethod("get"+replace,null);
                Method method3=null;

                //如果空则动态设置默认值
                if(null==method2.invoke(object,null)){
                    if(field.getType().isAssignableFrom(String.class)){
                        method3=Class.getMethod("set"+replace,field.getType());
                        method3.invoke(object,"");
                    }else if(field.getType().isAssignableFrom(BigDecimal.class)){
                        method3=Class.getMethod("set"+replace,field.getType());
                        method3.invoke(object,0);
                    }else if(field.getType().isAssignableFrom(Integer.class)){
                        method3=Class.getMethod("set"+replace,field.getType());
                        method3.invoke(object,0);
                    }else if(field.getType().isAssignableFrom(Double.class)){
                        method3=Class.getMethod("set"+replace,field.getType());
                        method3.invoke(object,0);
                    }
                }
            }catch (Exception e){}
        }
    }


    /***
    * @Description: 输出异常堆信息
    * @author: CYS
    * @date: 2022/3/8 10:32
    * @param throwable:
    * @Return: java.lang.String
    */
    public static String formatStackTrace(Throwable throwable) {
        if(throwable==null) return "";
        String rtn = null;
        try {
            Writer writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            throwable.printStackTrace(printWriter);
            printWriter.flush();
            writer.flush();
            rtn = writer.toString();
            printWriter.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
        }
        return rtn;
    }


}
